module.exports = class extends think.Service
{
    constructor(ctx)
    {
        super(ctx);
        this.isErr = false;
        this.errNo = 0;
        this.errMsg = 'ok';
        this.jData={};
    }

    getJudgerData(postDataObj)
    {
        this.jData.IsDelete='未删除';
        this.jData.Status='有效';
        this.jData.PK_JudgerGuid=think.uuid('v1');
        this.jData.Name = postDataObj.Name;
        this.jData.Tel = postDataObj.Tel;
        this.jData.UserName = postDataObj.UserName;
        this.jData.Password = postDataObj.Password;
        this._checkJData();
        return {
            'isErr': this.isErr,
            'errNo': this.errNo,
            'errMsg': this.errMsg
        }
    }

    async addData(){
        let a=await this.model('judger').add(this.jData);
        if(a==0){
            return {
                'isErr': false,
                'errNo':0,
                'errMsg':'数据添加成功'
            }
        }else{
            return {
                'isErr': true,
                'errNo':-1,
                'errMsg':'服务器繁忙,请稍后再试'
            }
        }

    }

    _checkJData()
    {
        for (let item in this.jData) {
          if(!think.isString(this.jData[item])){
              this.isErr = true;
              this.errNo = 52014;
              this.errMsg = item+"必须是字符串";
              return; 
          }
        }
        if (this.jData.Name.length < 2 || this.jData.Name.length >10)
        {
            this.isErr = true;
            this.errNo = 52010;
            this.errMsg = '姓名必须大于两个字并且小于10个字';
            return;
        }
        if (this.jData.Tel.length < 8 || this.jData.Tel.length>20)
        {
            this.isErr = true;
            this.errNo = 52011;
            this.errMsg = '电话号码格式错误';
            return;
        }
        if (this.jData.UserName.length < 2 ||this.jData.UserName.length >20)
        {
            this.isErr = true;
            this.errNo = 52012;
            this.errMsg = '用户名不能少于两个字符或过长';
            return;
        }
        if (this.jData.Password.length < 5 || this.jData.Password.length >64)
        {
            this.isErr = true;
            this.errNo = 52013;
            this.errMsg = '密码不能少于五个字符';
            return;
        }
    }


    async updateData(pk){
        let a=await this.model('judger').where({'PK_JudgerGuid':pk}).update(this.jData);
        if(a<=0){
            return {
                'isErr': true,
                'errNo':52018,
                'errMsg':'主键为该guid的评委不存在或该条记录已修改,请检查guid及需要修改的数据,或修改数据后重试'
            }
        }else{
            return {
                'isErr': false,
                'errNo':0,
                'errMsg':'修改成功'
            }
        }

    }
}